package com.ourteam.system.dao.impl;

import com.ourteam.system.dao.ISysDepartmentDAO;
import com.ourteam.system.dao.SysDepartment;
import com.ourteam.system.dao.SysDepartmentDAOQueryBean;

import net.dao.AbstractSpringDAO;
import net.dao.DataAccessObjectHelper;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.lang.StringUtils;

import org.springframework.jdbc.core.RowMapper;

import java.sql.ResultSet;
import java.sql.SQLException;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * Template Name : DAO服务对象
 * Template Version : 1
 * Template Last Modfiy Date : Fri Nov 17 11:00:11 CST 2017
 * Auto Generate Date : 2018-07-31 15:13:40
  * Warning ! This file will be overwrite when regenerate !
  *
 */
public class AbstractSysDepartmentDAOJdbcImpl extends AbstractSpringDAO
    implements ISysDepartmentDAO {
    public static final String[] ENTITY_FIELDS = new String[] {
            Id, ParentId, DepartmentName, Description, Status, ModifyTime,
            OperatorId, SortIndex, OperatorName, ParentDepartmentName
        };
    public static final String[] ENTITY_PROPERTIES = new String[] {
            _DataObjectProperty.Id, _DataObjectProperty.ParentId,
            _DataObjectProperty.DepartmentName, _DataObjectProperty.Description,
            _DataObjectProperty.Status, _DataObjectProperty.ModifyTime,
            _DataObjectProperty.OperatorId, _DataObjectProperty.SortIndex,
            _DataObjectProperty.OperatorName,
            _DataObjectProperty.ParentDepartmentName
        };
    public static final String[] ENTITY_PK_FIELDS = new String[] { Id };
    public static final String[] ENTITY_AUTO_GEN_PK_FIELDS = new String[] { Id };
    public static final String[] ENTITY_UNIQUE_FIELDS = new String[] {  };
    private static String relatedJoinSQL = null;
    private static String basicFilterSQL = null;
    private DataAccessObjectHelper<SysDepartment, SysDepartmentDAOQueryBean> daoHelper;

    public AbstractSysDepartmentDAOJdbcImpl() {
        super();
        daoHelper = new DataAccessObjectHelper<SysDepartment, SysDepartmentDAOQueryBean>(this);
    }

    protected String getEntityName() {
        return EntityName;
    }

    protected String[] getEntityFields() {
        return ENTITY_FIELDS;
    }

    protected String[] getEntityProperties() {
        return ENTITY_PROPERTIES;
    }

    protected String[] getEntityPKFields() {
        return ENTITY_PK_FIELDS;
    }

    protected String[] getEntityAutoGenPKFields() {
        return ENTITY_AUTO_GEN_PK_FIELDS;
    }

    protected String[] getEntityUniqueFields() {
        return ENTITY_UNIQUE_FIELDS;
    }

    protected String getBaiscQueryFilter() {
        if (basicFilterSQL == null) {
            StringBuffer basicFilterSQLbuffer = new StringBuffer();

            basicFilterSQL = basicFilterSQLbuffer.toString();
        }

        return basicFilterSQL;
    }

    protected String getRelatedJoinSQL() {
        if (relatedJoinSQL == null) {
            StringBuffer relatedJoinSQLBuffer = new StringBuffer();

            relatedJoinSQLBuffer.append(" LEFT JOIN ")
                                .append(com.ourteam.system.dao.ISysUserDAO.EntityName)
                                .append(" operator_user ");

            relatedJoinSQLBuffer.append(" ON ( ");

            relatedJoinSQLBuffer.append(StringUtils.replace(
                    com.ourteam.system.dao.ISysUserDAO.Id,
                    com.ourteam.system.dao.ISysUserDAO.EntityName + ".",
                    "operator_user."));
            relatedJoinSQLBuffer.append(" = ").append(OperatorId);

            relatedJoinSQLBuffer.append(" ) ");

            relatedJoinSQLBuffer.append(" LEFT JOIN ")
                                .append(com.ourteam.system.dao.ISysDepartmentDAO.EntityName)
                                .append(" PARENT_DEP ");

            relatedJoinSQLBuffer.append(" ON ( ");

            relatedJoinSQLBuffer.append(StringUtils.replace(
                    com.ourteam.system.dao.ISysDepartmentDAO.Id,
                    com.ourteam.system.dao.ISysDepartmentDAO.EntityName + ".",
                    "PARENT_DEP."));
            relatedJoinSQLBuffer.append(" = ").append(ParentId);

            relatedJoinSQLBuffer.append(" ) ");

            relatedJoinSQL = relatedJoinSQLBuffer.toString();
        }

        return relatedJoinSQL;
    }

    /**
         * Check Data Unique
         * @param aSysDepartment
         * @throws Exception
         */
    public boolean checkIsUnique(SysDepartment aSysDepartment)
        throws Exception {
        if (aSysDepartment == null) {
            return true;
        }

        List valueList = new ArrayList();

        List pkValueList = new ArrayList();

        pkValueList.add(aSysDepartment.getId());

        return isUnique(valueList.toArray(new Object[valueList.size()]),
            pkValueList.toArray(new Object[pkValueList.size()]));
    }

    /**
     * Insert Data
     * @param aSysDepartment
     * @throws Exception
     */
    public void insert(SysDepartment aSysDepartment) throws Exception {
        super.doInsert(aSysDepartment);
    }

    /**
         * Batch Insert Datas
         * @param aSysDepartment
         * @throws Exception
         */
    public void batchInsert(SysDepartment[] aSysDepartments)
        throws Exception {
        super.doBatchInsert(aSysDepartments);
    }

    /**
         * Delete Data
         * @param  final long id         *
         * @throws Exception
         */
    public void delete(final long id) throws Exception {
        super.doDeleteById(new Object[] { new Long(id) });
    }

    /**
     * Delete Datas
     * @param queryBean
     * @throws Exception
     */
    public void delete(SysDepartmentDAOQueryBean queryBean)
        throws Exception {
        super.doDeleteByQuery(queryBean);
    }

    public void delete(SysDepartment aSysDepartment) throws Exception {
        delete(aSysDepartment.getId());
    }

    /**
     * Update Data Selective
     * @param aSysDepartment
     * @throws Exception
     */
    public void update(SysDepartment aSysDepartment) throws Exception {
        if (aSysDepartment.getId() == 0) {
            throw new Exception(
                "Can not update object data whith out primary key value");
        }

        super.doUpdate(aSysDepartment);

        SysDepartment tempObj = findById(aSysDepartment.getId());

        if (tempObj != null) {
            PropertyUtils.copyProperties(aSysDepartment, tempObj);
        }
    }

    /**
     * Batch Update Datas
     * @param aSysDepartments[]
     * @throws Exception
     */
    public void batchUpdate(SysDepartment[] aSysDepartments)
        throws Exception {
        super.doBatchUpdate(aSysDepartments);
    }

    /**
         * Update Data By Condictions
         * @param aSysDepartment
         * @param aValueCriteria
         * @param aTransaction
         * @throws Exception
         */
    public void update(SysDepartment aSysDepartment,
        SysDepartmentDAOQueryBean aQueryBean) throws Exception {
        super.doUpdateByQuery(aSysDepartment, aQueryBean);
    }

    /**
     * Update Data By Condictions
     * @param aQueryBean
     * @throws Exception
     */
    public void update(SysDepartmentDAOQueryBean aQueryBean)
        throws Exception {
        super.doUpdateByQuery(aQueryBean);
    }

    protected void checkNullValue(SysDepartment aSysDepartment)
        throws Exception {
        if (aSysDepartment.getDepartmentName() == null) {
            throw new Exception(
                "The SysDepartment.departmentName value is not allow NULL");
        }

        if (aSysDepartment.getDescription() == null) {
            throw new Exception(
                "The SysDepartment.description value is not allow NULL");
        }

        if (aSysDepartment.getStatus() == null) {
            throw new Exception(
                "The SysDepartment.status value is not allow NULL");
        }

        if (aSysDepartment.getParentDepartmentName() == null) {
            throw new Exception(
                "The SysDepartment.parentDepartmentName value is not allow NULL");
        }

        if (aSysDepartment.getModifyTime() == null) {
            throw new Exception(
                "The SysDepartment.modifyTime value is not allow NULL");
        }

        if (aSysDepartment.getOperatorName() == null) {
            throw new Exception(
                "The SysDepartment.operatorName value is not allow NULL");
        }
    }

    /**
     * Get Data By ID
     * @param  final long id         * @return  ${businessObject.name}
     * @throws Exception
     */
    public SysDepartment findById(final long id) throws Exception {
        SysDepartmentDAOQueryBean queryBean = new SysDepartmentDAOQueryBean();

        SysDepartmentDAOQueryBean.Criteria criteria = queryBean.createCriteria();

        criteria.andIdEqualTo(id);

        //queryBean.setDistinct(true);
        SysDepartment[] SysDepartments = query(queryBean);

        if (SysDepartments.length == 0) {
            return null;
        } else {
            return SysDepartments[0];
        }
    }

    /**
         * Query Data
         * @param queryBean
         * @return SysDepartment[]
         * @throws Exception
         */
    public SysDepartment[] query(SysDepartmentDAOQueryBean queryBean)
        throws Exception {
        List result = super.doQuery(queryBean, new JdbcSysDepartmentRowMapper());

        return (SysDepartment[]) result.toArray(new SysDepartment[result.size()]);
    }

    /**
     * Query ${businessObject.name} Selective
     * @param queryBean
     * @return Map[]
     * @model
     * @throws Exception
     */
    public Map<String, Object>[] querySelective(
        SysDepartmentDAOQueryBean queryBean) throws Exception {
        if (queryBean.getSelectProperties().isEmpty()) {
            queryBean.addAllSelectProperties();
        }

        return super.doQuerySelective(queryBean);
    }

    /**
     * Query Data Count
     * @param queryBean
     * @return int
     * @throws Exception
     */
    public int queryCount(SysDepartmentDAOQueryBean queryBean)
        throws Exception {
        return super.doQueryCount(queryBean);
    }

    public void synchronize(SysDepartment[] aSysDepartments,
        SysDepartmentDAOQueryBean queryBean) throws Exception {
        daoHelper.synchronize(aSysDepartments, queryBean);
    }

    public void synchronize(SysDepartment[] aSysDepartments,
        SysDepartmentDAOQueryBean queryBean,
        net.dao.IDataAccessInterface.ISynchronizeCallback<SysDepartment> callBack)
        throws Exception {
        daoHelper.synchronize(aSysDepartments, queryBean, callBack);
    }

    public static class JdbcSysDepartmentRowMapper implements RowMapper<SysDepartment> {
        public SysDepartment mapRow(ResultSet resultSet, int arg1)
            throws SQLException {
            SysDepartment object = new SysDepartment();

            int columnIndex = 1;

            int __dataType;

            try {
                Object id = null;

                id = resultSet.getObject(columnIndex++);

                object.setIdNull(resultSet.getBoolean(columnIndex++));

                if (id != null) {
                    setBeanValue(object, _DataObjectProperty.Id, id);
                }

                Object parentId = null;

                parentId = resultSet.getObject(columnIndex++);

                object.setParentIdNull(resultSet.getBoolean(columnIndex++));

                if (parentId != null) {
                    setBeanValue(object, _DataObjectProperty.ParentId, parentId);
                }

                Object departmentName = null;

                departmentName = resultSet.getObject(columnIndex++);

                object.setDepartmentNameNull(resultSet.getBoolean(columnIndex++));

                if (departmentName != null) {
                    setBeanValue(object, _DataObjectProperty.DepartmentName,
                        departmentName);
                }

                Object description = null;

                description = resultSet.getObject(columnIndex++);

                object.setDescriptionNull(resultSet.getBoolean(columnIndex++));

                if (description != null) {
                    setBeanValue(object, _DataObjectProperty.Description,
                        description);
                }

                Object status = null;

                status = resultSet.getObject(columnIndex++);

                object.setStatusNull(resultSet.getBoolean(columnIndex++));

                if (status != null) {
                    setBeanValue(object, _DataObjectProperty.Status, status);
                }

                Object modifyTime = null;

                __dataType = resultSet.getMetaData().getColumnType(columnIndex);

                if ((__dataType == java.sql.Types.DATE) ||
                        (__dataType == java.sql.Types.TIME) ||
                        (__dataType == java.sql.Types.TIMESTAMP)) {
                    modifyTime = resultSet.getTimestamp(columnIndex++);
                } else {
                    modifyTime = resultSet.getObject(columnIndex++);
                }

                object.setModifyTimeNull(resultSet.getBoolean(columnIndex++));

                if (modifyTime != null) {
                    setBeanValue(object, _DataObjectProperty.ModifyTime,
                        modifyTime);
                }

                Object operatorId = null;

                operatorId = resultSet.getObject(columnIndex++);

                object.setOperatorIdNull(resultSet.getBoolean(columnIndex++));

                if (operatorId != null) {
                    setBeanValue(object, _DataObjectProperty.OperatorId,
                        operatorId);
                }

                Object sortIndex = null;

                sortIndex = resultSet.getObject(columnIndex++);

                object.setSortIndexNull(resultSet.getBoolean(columnIndex++));

                if (sortIndex != null) {
                    setBeanValue(object, _DataObjectProperty.SortIndex,
                        sortIndex);
                }

                Object operatorName = null;

                operatorName = resultSet.getObject(columnIndex++);

                object.setOperatorNameNull(resultSet.getBoolean(columnIndex++));

                if (operatorName != null) {
                    setBeanValue(object, _DataObjectProperty.OperatorName,
                        operatorName);
                }

                Object parentDepartmentName = null;

                parentDepartmentName = resultSet.getObject(columnIndex++);

                object.setParentDepartmentNameNull(resultSet.getBoolean(
                        columnIndex++));

                if (parentDepartmentName != null) {
                    setBeanValue(object,
                        _DataObjectProperty.ParentDepartmentName,
                        parentDepartmentName);
                }

                return object;
            } catch (Exception e) {
                throw new SQLException(e.getMessage());
            }
        }
    }
}
